home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Magazine / C_Tutorial / Part-2 / mouse1.c < prev    next >
C/C++ Source or Header  |  1997-07-01  |  2KB  |  93 lines

  1. #include<exec/libraries.h>
  2. #include<intuition/intuition.h>
  3. #include<utility/tagitem.h>
  4. #include<graphics/text.h>
  5.  
  6. #include<string.h>
  7.  
  8. #include<clib/exec_protos.h>
  9. #include<clib/graphics_protos.h>
  10. #include<clib/intuition_protos.h>
  11.  
  12. struct Library* GfxBase;
  13. struct Library* IntuitionBase;
  14.  
  15. /* Need to give a prototype for our message handling function */
  16. void handleIDCMP(struct Window*);
  17.  
  18. void main()
  19. {
  20.     /* Open libraries... */
  21.     if(GfxBase = OpenLibrary("graphics.library",36))
  22.     {
  23.         if(IntuitionBase = OpenLibrary("intuition.library",36))
  24.         {
  25.             /* Open our window */
  26.             struct Window* win;
  27.             if(win = OpenWindowTags(NULL,
  28.                                                             WA_Left,        20,
  29.                                                             WA_Top,            20,
  30.                                                             WA_Width,        200,
  31.                                                             WA_Height,    100,
  32.                                                             WA_Flags,        WFLG_CLOSEGADGET | WFLG_DRAGBAR | WFLG_REPORTMOUSE,
  33.                                                             WA_IDCMP,        IDCMP_CLOSEWINDOW | IDCMP_MOUSEBUTTONS | IDCMP_MOUSEMOVE,
  34.                                                             TAG_DONE,        0))
  35.             {
  36.                 /* If window opened, handle its IDCMP messages */
  37.                 handleIDCMP(win);
  38.                 CloseWindow(win);
  39.             }
  40.             CloseLibrary(IntuitionBase);
  41.         }
  42.         CloseLibrary(GfxBase);
  43.     }
  44. }
  45.  
  46. /* Our message handling code */
  47. void handleIDCMP(struct Window* win)
  48. {
  49.     char* text = "Hello World!";
  50.     int going = TRUE;
  51.     int drawing = FALSE;
  52.     SetAPen(win->RPort, 1);
  53.     /* Loop, waiting for messages, until the close gadget clicked */
  54.     while(going)
  55.     {
  56.         struct IntuiMessage* intuimsg;
  57.         /* Wait for messages to arrive */
  58.         WaitPort(win->UserPort);
  59.         /* Messages have arrived: loop through all of them */
  60.         while(intuimsg = (struct IntuiMessage*)GetMsg(win->UserPort))
  61.         {
  62.             /* Act on this message... */
  63.             switch(intuimsg->Class)
  64.             {
  65.             case IDCMP_MOUSEBUTTONS:
  66.                 switch(intuimsg->Code)
  67.                 {
  68.                 case SELECTDOWN:
  69.                     drawing = TRUE;
  70.                     break;
  71.                 case SELECTUP:
  72.                     drawing = FALSE;
  73.                     break;
  74.                 }
  75.                 /* Omit the break to draw on click, too */
  76.                 break;
  77.             case IDCMP_MOUSEMOVE:
  78.                 if(drawing)
  79.                 {
  80.                     Move(win->RPort, intuimsg->MouseX, intuimsg->MouseY);
  81.                     Text(win->RPort, text, strlen(text));
  82.                 }
  83.                 break;
  84.             case IDCMP_CLOSEWINDOW:
  85.                 going = FALSE;
  86.                 break;
  87.             }
  88.             /* Reply when finished with message */
  89.             ReplyMsg((struct Message*)intuimsg);
  90.         }
  91.   }
  92. }
  93.